import { NavigationFailure, RouteLocationNormalized } from "vue-router";
import { RouterExtraOptions } from "./app-router-type.ts";

export default (
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  appRouterOptions: RouterExtraOptions,
  failure?: NavigationFailure | void,
) => {
  const showLoading =
    typeof appRouterOptions.showLoading === "function"
      ? appRouterOptions.showLoading()
      : appRouterOptions.showLoading;
  if (showLoading) {
    appRouterOptions.hideLoadingFunction(!failure);
  }

  if (failure) {
    console.error(failure);
  }

  return appRouterOptions.afterNavigation(to, from, failure);
};
